package com.education.interceptor;

import com.education.common.Const;
import com.education.common.RequestHolder;
import com.education.pojo.Admin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 林心晨
 * @Title:
 * @Package
 * @Description:
 * @date create in 5.3  10:39
 */
public class LoginInterceptor implements HandlerInterceptor {

    private static Logger logger = LoggerFactory.getLogger(LoginInterceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        logger.info(request.getRequestURL().toString());
        Admin currentAdmin = (Admin) request.getSession().getAttribute(Const.CURRENT_Admin);
        if (currentAdmin == null){
            response.sendRedirect(request.getContextPath() + "/manage/login");
            return false ;
        }
        RequestHolder.add(currentAdmin);
        RequestHolder.add(request);
        return true ;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
